﻿//#define SKIN_DEBUG

using LogSystem;
using System;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEngine;
using Yoozoo.Core.Extensions.Engine;

namespace Yoozoo.Gameplay.RTS
{
    public class GPUSkinningSimplePlayer : MonoBehaviour
    {
        /// <summary>
        /// ai 实体表现
        /// </summary>
        [SerializeField]
        protected GameObject _roleObj;
        
        /// <summary>
        /// GPU Skinning
        /// </summary>
        [SerializeField]
        protected ModelInstaning model;
        
        /// <summary>
        /// 可以看见这个物体的相机标记 (HCamera上设置的标记)
        /// </summary>
        public string CullingCameraMark = "City2dCamera;TimelineCamera";

        /// <summary>
        /// 裁剪层
        /// </summary>
        public string CullingLayer = "city";
        
        /// <summary>
        /// 循环动画名称
        /// </summary>
        public string LoopAnimationName = "Walk";
        
        /// <summary>
        /// 动画播放速度
        /// </summary>
        public float Speed = 1;
        
        /// <summary>
        /// 
        /// </summary>
        private bool IsInit = false;

        void Update()
        {
            if (!IsInit && GPUInstancingManager.GetInstance().IsInitialized())
            {
                IsInit = true;
                Init();
            }
        }

        void Init()
        {
            if (!_roleObj)
            {
                _roleObj = transform.GetChild(0).gameObject;
            }
            if (!model)
            {
                model = _roleObj.GetComponentInChildren<ModelInstaning>(true);
            }
            model.Init();
            model.SetCullingCameraMark(CullingCameraMark);

            var instanceId = model.SetConfig(model.poolKey, 100000, 1);
            model.SetMainColor(instanceId, Color.white);
                    
            model.SetPosition(_roleObj.transform.position);
            model.SetRotation(_roleObj.transform.eulerAngles);
            model.SetScale(model.localScale, false);
                    
            model.SetAnimatorEnabled(true);
            model.SetRendererEnabled(true);
            model.SetCullingLayer(LayerMask.NameToLayer(CullingLayer));
            
            model.SetTrigger(LoopAnimationName);
            model.SetAnimatorSpeed(Speed);
        }
        
    }
}
